package com.content.view.Frame.MainPanel.MPOnlineResVideo
{
	import com.clerkq.CircleList.data.ThumbHVO;
	import com.clerkq.CircleList.data.ThumbVVO;
	import com.content.model.Collection.ItemGroup;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.model.OnlineRes.OnlineResVO_Video;
	import com.content.view.Component.OnlineRes.OnlineResEvent;
	import com.content.view.Component.OnlineRes.SPCart;
	import com.content.view.Component.OnlineRes.SPResDetail;
	import com.content.view.Component.OnlineRes.SPShelf;
	import com.content.view.Frame.MainPanel.MPBase;
	import com.content.view.Share.Player.VideoPlayer;
	import com.content.view.Share.Player.VideoPlayer_Mediator;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	import com.library.utils.Geometry;
	import com.library.utils.TimeUtil;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	public final class MPOnlineResVideo extends MPBase
	{
		private var _spShelf:SPShelf;
		private var _spCart:SPCart;
		private var _playerVideo:VideoPlayer_Mediator;
		
		private var _spResDetail:SPResDetail;	// 浮层
		
		public function MPOnlineResVideo(	marginTop:int,
										marginBottom:int,
										marginLeft:int,
										marginRight:int,
										cBarWidth:int, 
										playerVideo:VideoPlayer_Mediator,
										bgSkin:Sprite = null)
		{
			super(	
				this,
				marginTop,
				marginBottom,
				marginLeft,
				marginRight,
				cBarWidth, 
				bgSkin);
			
			_playerVideo = playerVideo;
			
			_spShelf = new SPShelf();
			_spCart = new SPCart();
			_spResDetail = new SPResDetail();
			_container.addChild(_spShelf);
			_container.addChild(_spCart);
			_container.addChild(_spResDetail);
			_spResDetail.visible = false;
			_spResDetail.alpha = 0;
			
			_spShelf.addEventListener(SPShelf.CHOOSE_RES, OnChooseRes);
			_spResDetail.addEventListener(SPResDetail.DOWNLOAD, OnDownloadRes);
			_spResDetail.addEventListener(SPResDetail.CLOSE, OnCloseRes);
			_spCart.addEventListener(SPCart.PLAY_RES, OnPlayRes);
		}
		
		override protected function SetLayoutMP(areaWidth:int, areaHeight:int):void{
			_spShelf.SetLayout( int(areaWidth*1.0), int(areaHeight*0.65) );
			_spCart.SetLayout( int(areaWidth*1.0), int(areaHeight*0.35) );
			_spResDetail.SetLayout( areaWidth, areaHeight );
			
			_spCart.y = int(areaHeight*0.65);
		}
		
		/* Event Handler
		* --------------------------------------------------- */
		private function OnChooseRes(evt:OnlineResEvent):void{
			_spResDetail.UpdateContent(evt.OLResVO);
			TweenLite.to(_spResDetail, 0.3, {autoAlpha:1});
		}
		private function OnDownloadRes(evt:OnlineResEvent):void{
			_spCart.AddToCart(evt.OLResVO);
			TweenLite.to(_spResDetail, 0.3, {autoAlpha:0});
		}
		private function OnCloseRes(evt:DataEvent):void{
			TweenLite.to(_spResDetail, 0.3, {autoAlpha:0});
		}
		private function OnPlayRes(evt:OnlineResEvent):void{
			_playerVideo.Play(evt.OLResVO as OnlineResVO_Video);
		}
		
		/* Public Method
		* ---------------------------------------------------- */
		public function UpdateCategory(xml:XML):void{
			var cmdName:String = xml.head.command;
			
			var i:uint = 0;
			var cateItem:XML;
			var cateVOGroup:ItemGroup = new ItemGroup();
			for each (cateItem in xml.body.category.item)
			{
				var cVO:ThumbVVO = new ThumbVVO();
				cVO.CateID = cateItem.@id;
				cVO.Text = cateItem.@text;
				cVO.Index = i;
				cateVOGroup.AddVO(cVO);
				i++;
			}
			_spShelf.FillCircleList(cateVOGroup);
		}
		
		public function UpdateResource(xml:XML):void{
			var cmdName:String = xml.head.command;
			
			var resItem:XML;	
			var resVOGroup:ItemGroup = new ItemGroup();
			for each (resItem in xml.body.shelf.item)
			{
				var rVO:OnlineResVO_Video = new OnlineResVO_Video();
				rVO.Resource = xml.body.resource;
				rVO.ResID = resItem.@id;
				rVO.Name = resItem.info.(@id=="name")
				rVO.Description = resItem.info.(@id=="description")
				rVO.Author = resItem.info.(@id=="author")
				rVO.Publisher = resItem.info.(@id=="publisher")
				rVO.PublishDate = TimeUtil.DateStringToObject(resItem.info.(@id=="publishdate"));
				rVO.Star = int(resItem.info.(@id=="star"));
				rVO.Purchased = resItem.info.(@id=="purchased").toLowerCase() == "true"?true:false;
				rVO.CoverURL = resItem.info.(@id=="coverurl")
				rVO.CoverURLSmall = resItem.info.(@id=="coverurlsmall")
				rVO.DownUrl = resItem.info.(@id=="downurl")
				rVO.Actor = resItem.info.(@id=="actor")
				rVO.Adaptor = resItem.info.(@id=="adaptor")
				rVO.Director = resItem.info.(@id=="director")
				rVO.Setcount = resItem.info.(@id=="setcount")
				resVOGroup.AddVO(rVO);
			}
			_spShelf.FillResWrapper(resVOGroup);
		}
		
		public function UpdateCart(xml:XML):void{
			var cmdName:String = xml.head.command;
			
			var j:uint = 0;
			var downItem:XML;	
			var downVOGroup:ItemGroup = new ItemGroup();
			for each (downItem in xml.body.shelf.item)
			{
				var dVO:ThumbHVO = new ThumbHVO();
				dVO.Resource = downItem.@resource;
				dVO.ResID = downItem.@id;
				dVO.Name = downItem.info.(@id=="name")
				dVO.Description = downItem.info.(@id=="description")
				dVO.Author = downItem.info.(@id=="author")
				dVO.Publisher = downItem.info.(@id=="publisher")
				dVO.PublishDate = TimeUtil.DateStringToObject(downItem.info.(@id=="publishdate"));
				dVO.Star = int(downItem.info.(@id=="star"));
				dVO.Purchased = downItem.info.(@id=="purchased").toLowerCase() == "true"?true:false;
				dVO.CoverURL = downItem.info.(@id=="coverurl")
				dVO.CoverURLSmall = downItem.info.(@id=="coverurlsmall")
				dVO.DownUrl = downItem.info.(@id=="downurl")
				dVO.Index = j;
				downVOGroup.AddVO(dVO);
				j++;
			}
			_spCart.FillResWrapper(downVOGroup);
		}
		
		override public function Dispose():void
		{
			_spShelf.removeEventListener(SPShelf.CHOOSE_RES, OnChooseRes);
			_spResDetail.removeEventListener(SPResDetail.DOWNLOAD, OnDownloadRes);
			_spResDetail.removeEventListener(SPResDetail.CLOSE, OnCloseRes);
			_spCart.removeEventListener(SPCart.PLAY_RES, OnPlayRes);
			
			_spShelf.Dispose();
			_spCart.Dispose();
			_spResDetail.Dispose();
		}
	}
}